package com.example.shellcollector.MyAnimate;

import android.content.pm.PackageManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.shellcollector.Entity.InstalledAppUsage;
import com.example.shellcollector.R;

import java.util.List;

public class MyCountAdapter extends RecyclerView.Adapter<MyCountAdapter.MyViewHolder> {

    private List<InstalledAppUsage> mDataset;
    private PackageManager manager;
    private MyCountAdapter.ItemClickListener mItemClickListener;
    private static int max=100;

    public interface ItemClickListener {
        public void onItemClick(int position);
    }

    public void setOnItemClickListener(MyCountAdapter.ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        public ImageView imageView;
        private MyProgressBar progressBar;

        public MyViewHolder(View v) {
            super(v);
            imageView = (ImageView) v.findViewById(R.id.appcounticon);
            progressBar =(MyProgressBar)v.findViewById(R.id.appcount);


        }
    }

    public MyCountAdapter(List<InstalledAppUsage> myDataset, PackageManager packageManager) {
        mDataset = myDataset;
        manager = packageManager;
    }

    @Override
    public MyCountAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.appcount_item, parent, false);


        MyCountAdapter.MyViewHolder vh = new MyCountAdapter.MyViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(MyCountAdapter.MyViewHolder holder, int position) {
        InstalledAppUsage app = mDataset.get(position);
        if (position==0)
            max=(app.getLaunchCount()/10+1)*10;
        holder.imageView.setImageDrawable(app.getAppIcon(manager));
        holder.progressBar.setColor(0xFFF9630E);
        holder.progressBar.setProgress(app.getLaunchCount(),max);


        if (mItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    mItemClickListener.onItemClick(position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mDataset.size();
    }
}